package cn.jwelll.bxcstock.base;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import cn.jwelll.bxcstock.R;
import cn.jwelll.bxcstock.ui.dialog.DialogUtils;


/**
 * 采用MVP模式的基础Activity，View层，创建Presenter
 */
public abstract class MVPBaseActivity<V, P extends BasePresenter<V>> extends BaseActivity {
    protected P mPresenter; // Presenter对象

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mPresenter = createPresenter();
        mPresenter.attachView((V) this);
        mPresenter.start();
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    protected abstract P createPresenter();

    private Dialog loading;
    private Dialog dialog;

    public void dismissProgress() {
        if (loading != null) {
            if (loading.isShowing())
                loading.dismiss();
        }
    }

    public void showFailed(String message) {
        if (loading != null) {
            Log.e("msg", "loading" + loading);
            loading.dismiss();
        }
        showDialog(message);
    }

    public void showDialog(String message) {
        dialog = new DialogUtils.Builder(this)
                .setOnlyPositive()
                .setMessage(message)
                .creat();
        dialog.show();
    }

    protected void showProgress() {
        showLoading(getResources().getString(R.string.progress_tip_default));
    }

    public void showLoading(String message) {
        loading = new DialogUtils.Builder(this)
                .setType(DialogUtils.LOADING)
                .setOutTouch(false)
                .setMessage(message)
                .creat();
        loading.setCancelable(true);
        loading.show();
    }
}
